package com.css.dzj.dzjap.modules.publick.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseJSService;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.publick.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/publick")
public class PublicController {
     //公开公告
    @Autowired
    private PublicService publicService;

    //专家信用信息表
    @Autowired
    private ExpertCreditService expertCreditService;

    //评价单位信用信息表
    @Autowired
    private EnterpriseService enterpriseService;

    //建设单位信用信用表
    @Autowired
    private EnterpriseJSService enterpriseJsService;

    @Autowired
    BaseService baseService;


    /**
     * 通过主键Id查询公开公告表
     */
    @GetMapping(value = "/getPublick")
    public R getPublick(@RequestParam("publicId") String publicId){
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",publicService.getPublick(publicId));
    }


    /**
     * 添加公开公告表
     */
    @PostMapping(value = "/addPublick")
    public R addPublick(@RequestBody ApPublicEntity pub) {
        System.out.println(pub.getTitle());

        if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                || StringUtils.isEmpty(pub.getPublicState())) {

            return R.error("必填字段不能为空");
        }
        String flag = publicService.addPublick(pub);
        if (!"0".equals(flag)) {
            R r = new R();
            r.put("code", 200);
            r.put("msg", "操作成功");
            r.ok().put("data", flag);
            baseService.logSuccess(3,"添加成功","2");
            return r;
        } else {
            baseService.logFailed(3,"添加失败","2");
            return R.error("添加失败");
        }


    }

    /**
     * 修改公开公告表
     */
    @PostMapping(value = "/updatePublick")
    public R updatePublick(@RequestBody ApPublicEntity pub ){

        try {
            ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
            BeanUtil.copyBeanNotNullToBean(pub, aPub);
            publicService.updatePublic(aPub);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 审批公开公告表
     */
    @PostMapping(value = "/approvalPublick")
    public R approvalPublick(@RequestBody ApPublicEntity pub ){

        try {
            if(StringUtils.isEmpty(pub.getPublishType())){
                return  R.error("必选字段不能为空！");
            }
            ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
            BeanUtil.copyBeanNotNullToBean(pub, aPub);
            if("1".equals(aPub.getPublishType())){
                aPub.setPublishTime(new Date());
            }
            publicService.approvalPublick(aPub);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }



    /**
     * 删除公开公告表
     */
    @PostMapping(value = "/deletePublick")
    public R deletePublick(@RequestParam("publicId") String publicId){

        System.out.println(publicId);
        try{

            if(publicService.deletePublick(publicId)){
                baseService.logSuccess(3,"删除成功","2");
                return R.ok("操作成功!");
            }else{
                baseService.logFailed(3,"删除失败","2");
                return  R.error("操作失败!");
            }
        }catch (Exception e){
            baseService.logFailed(3,"删除失败","2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 分页查询所有公开公告表
     */
    @GetMapping(value = "/getAllPublick")
    public R getAllPublick(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize
                                                                       ) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);

       /* QueryResults page = publicService.getAllPublick(pageRequest);
        List<ApPublicEntity> list = page.getResults();
        List<ApPublicEntity> newlist = new ArrayList<>();
        for (ApPublicEntity uplist : list) {
            ApPublicEntity pub  = new ApPublicEntity();
            pub.setPublicId(uplist.getPublicId());
            pub.setPublicState(uplist.getPublicState());
            pub.setDelFlag(uplist.getDelFlag());
            Date createTime =null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                createTime = sf.parse(uplist.getCreateTime().toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            pub.setCreateTime(createTime);
            pub.setRevokeg(uplist.getRevokeg());
            pub.setContent(uplist.getContent());
            pub.setCreateUser(uplist.getCreateUser());
            pub.setEnclosure(uplist.getEnclosure());
            pub.setPublicType(uplist.getPublicType());
            pub.setTitle(uplist.getTitle());
            newlist.add(pub);
        }

        QueryResults qr  = new QueryResults(newlist,page.getLimit(),page.getOffset(),page.getTotal());*/
       baseService.logSuccess(3,"查询成功","2");
       return R.ok().put("data",publicService.getAllPublick(pageRequest));
    }


    /**
     * 撤销公开公告表
     */
    @PostMapping(value = "/updateRePublick")
    public R updateRePublick(@RequestBody ApPublicEntity pub ){

        try {
            if("0".equals(pub.getPublicState())){
                return R.error("未公开状态，不能撤销!");
            }else {
                ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
                BeanUtil.copyBeanNotNullToBean(pub, aPub);
                aPub.setRevokeg(Constant.REVOKE_YES);
                aPub.setPublicState("0");
                publicService.update(aPub);
                baseService.logSuccess(3,"撤销成功","2");
                return R.ok();
            }
        } catch (Exception e) {
            baseService.logFailed(3,"撤销失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询公开公告表
     */
    @GetMapping(value ="/getAllConPublick")
    public R getAllConPublick(@RequestParam String title,
                              @RequestParam(required = false) String createUser,
                              @RequestParam(required = false) String publicType,
                              @RequestParam(required = false) String publicState,
                              @RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize){


        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        /*Timestamp begin = (Timestamp)beginTime;
        Timestamp end = (Timestamp)endTime;*/
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", publicService.getAllConPublick(pageRequest,title,createUser,publicType,publicState));
    }

    /**
     *  添加拟制公告信息通过关联专家ID
     */
    @PostMapping(value = "/addGlPublick")
    public R addGlPublick(@RequestBody ApPublicEntity pub) {

        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApExpertCreditEntity expertCred = expertCreditService.getExpertCred(pub.getRelationId());
            if(expertCred!=null){
                expertCred.setExpertNotice("1");
                expertCreditService.update(expertCred);
            }
            String flag = publicService.addGlPublick(pub);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            return  R.error(e.getMessage());
        }
    }


    /**
     * 添加黑名单拟制公告信息通过关联专家ID
     */
    @PostMapping(value = "/addGlRePublick")
    public R addGlRePublick(@RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApExpertCreditEntity expertCred = expertCreditService.getReExpertCred(pub.getRelationId());
            expertCred.setExpertNotice("1");
            expertCreditService.update(expertCred);

            String flag = publicService.addGlRePublick(pub);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }

    /**
     * 通过关联ID查询公开公告表
     */
    @GetMapping(value = "/getGlPublick")
    public R getGlPublick(@RequestParam String relationId){
        ApPublicEntity glPublick = publicService.getGlPublick(relationId);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",glPublick);
    }


    /**
     * 通过关联ID类型查询公开公告表
     */
    @GetMapping(value = "/getGlTYPublick")
    public R getGlTYPublick(@RequestParam String relationId,
                            @RequestParam String enterType){
        baseService.logSuccess(3,"添加成功","2");
        return R.ok().put("data",publicService.getGlTYPublick(relationId,enterType));
    }

    /**
     *  添加拟制公告信息通过关联评价单位ID
     */
    @PostMapping(value = "/addGlPjPublick/{enterpriseCredittype}")
    public R addGlPjPublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseService.getEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseService.update(enterPrise);

            String flag = publicService.addGlPjPublick(pub,enterpriseCredittype);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联评价单位Id
     */
    @PostMapping(value = "/addGlPjRePublick/{enterpriseCredittype}")
    public R addGlPjRePublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseService.getReEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseService.update(enterPrise);

            String flag = publicService.addGlPjRePublick(pub,enterpriseCredittype);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }


    /**
     *  添加拟制公告信息通过关联建设单位ID
     */
    @PostMapping(value = "/addGlJsPublick/{enterpriseCredittype}")
    public R addGlJsPublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseJsService.getEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseJsService.update(enterPrise);

            String flag = publicService.addGlJsPublick(pub,enterpriseCredittype);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logSuccess(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logSuccess(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联建设单位Id
     */
    @PostMapping(value = "/addGlJsRePublick/{enterpriseCredittype}")
    public R addGlJsRePublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseJsService.getReEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseJsService.update(enterPrise);

            String flag = publicService.addGlJsRePublick(pub,enterpriseCredittype);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }
    }



    /**
     * 带有标题参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @GetMapping("/titleList")
    public List titleList(@RequestParam String title,@RequestParam String publicType,@RequestParam String isPublic){

        try{
            List<ApPublicEntity> titleList =  publicService.titleList(title,publicType,isPublic);
            //baseService.logSuccess(3,"查询成功","2");
            return titleList;

        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return null;
        }

    }


    /**
     * 不带参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @GetMapping("/allList")
    public List allList(@RequestParam String publicType,@RequestParam String isPublic){

        try{
            List<ApPublicEntity> allList =  publicService.allList(publicType,isPublic);
            //baseService.logSuccess(3,"查询成功","2");
            return allList;
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return null;
        }
    }

    /**
     * 通过id查询详情
     * @author wt
     * @date 2020\3\12 0012
     */
    @GetMapping("/publicEntity")
    public ApPublicEntity getEntity(@RequestParam("id") String id){

        //baseService.logSuccess(3,"查询成功","2");
        ApPublicEntity  publicEntity = publicService.getEntity(id);
        return publicEntity;
    }

}
